package com.ar.network;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.exartisan.bean.Data;
import com.loopj.android.http.RequestParams;

public class StupidParametersBuilder {

	public StupidParametersBuilder() {
		
	}
	
	public static RequestParams BuildParams(String data){
		RequestParams params = new RequestParams();
		params.put(NetworkUtility.DATA, data);
		return params;
	}
	
	public static String BuildLoginData(String email, String passwords, String type){
		JSONObject data = new JSONObject();
		data.put(NetworkUtility.HANDLE, NetworkUtility.HANDLE_LOGIN);
		data.put(NetworkUtility.EMAIL, email);
		data.put(NetworkUtility.PASSWORDS, passwords);
		data.put(NetworkUtility.TYPE, type);
		return data.toString();
	}
	
	public static String BuildRegisterData(String email, String password, String phone, String birth_day, String address, String area_id, String type){
		JSONObject data = new JSONObject();
		JSONObject userInfo = new JSONObject();
		
		userInfo.put(NetworkUtility.PASSWORDS, password);
		userInfo.put(NetworkUtility.PHONE, phone);
		userInfo.put(NetworkUtility.EMAIL, email);
		userInfo.put(NetworkUtility.BIRTH_DAY, birth_day);
		userInfo.put(NetworkUtility.ADDRESS, address);
		userInfo.put(NetworkUtility.AREA_ID, area_id);
		
		data.put(NetworkUtility.HANDLE, NetworkUtility.HANDLE_REGISTER);
		data.put(NetworkUtility.TYPE, type);
		data.put(NetworkUtility.USER_INFOR, userInfo);
		
		return data.toString();
	}
	
	
	public static String BuildUpdateData(String user_id, String full_name, String phone, String birthday, String address, String area_id, String type){
		JSONObject data = new JSONObject();
		JSONObject userInfo = new JSONObject();
		userInfo.put(NetworkUtility.FULL_NAME, full_name);
		userInfo.put(NetworkUtility.PHONE, phone);
		userInfo.put(NetworkUtility.BIRTHDAY, birthday);
		userInfo.put(NetworkUtility.ADDRESS, address);
		userInfo.put(NetworkUtility.AREA_ID, area_id);
		
		data.put(NetworkUtility.HANDLE, NetworkUtility.HANDLE_REGISTER);
		data.put(NetworkUtility.USER_ID, user_id);
		data.put(NetworkUtility.USER_INFO, userInfo);
		
		return data.toString();
	}
	
	public static String BuildLoadSurveyData(String user_id){
		JSONObject data = new JSONObject();
		data.put(NetworkUtility.HANDLE, NetworkUtility.HANDLE_LOAD_SURVEY);
		data.put(NetworkUtility.USER_ID, user_id);
		return data.toString();
	}
	
	public static String BuildLoadQuestionData(String user_id, String contest_id){
		JSONObject data = new JSONObject();
		data.put(NetworkUtility.HANDLE, NetworkUtility.HANDLE_LOAD_QUESTION);
		data.put(NetworkUtility.USER_ID, user_id);
		data.put(NetworkUtility.CONTEST_ID, contest_id);
		return data.toString();
	}
	
	public static String BuildLoadSavedData(String user_id, String contest_id){
		JSONObject data = new JSONObject();
		data.put(NetworkUtility.HANDLE, NetworkUtility.HANDLE_LOAD_SAVED_DATA);
		data.put(NetworkUtility.USER_ID, user_id);
		data.put(NetworkUtility.CONTEST_ID, contest_id);
		return data.toString();
	}
	
	public static RequestParams BuildSaveParams(String email, String contest_id, ArrayList<Data> user_data){
		RequestParams params = new RequestParams();
		JSONObject data = new JSONObject();
		for (Data d : user_data) {
			data.put(NetworkUtility.QUESTION_ID, d.getQuestionID());
			data.put(NetworkUtility.ANSWER, d.getUserAnswer());
		}
		params.put(NetworkUtility.EMAIL, email);
		params.put(NetworkUtility.CONTEST_ID, contest_id);
		params.put(NetworkUtility.UPLOAD_DATA, data.toString());
		return params;
	}
	
	public static RequestParams BuildSubmitParams(String email, String contest_id, ArrayList<Data> user_data){
		RequestParams params = new RequestParams();
		JSONObject data = new JSONObject();
		for (Data d : user_data) {
			data.put(NetworkUtility.QUESTION_ID, d.getQuestionID());
			data.put(NetworkUtility.ANSWER, d.getUserAnswer());
		}
		params.put(NetworkUtility.EMAIL, email);
		params.put(NetworkUtility.CONTEST_ID, contest_id);
		params.put(NetworkUtility.UPLOAD_DATA, data.toString());
		return params;
	}
	
	public static String BuildSumitContest(String user_id, String contest_id, ArrayList<QuestionRequest> submit_data){
		JSONObject data = new JSONObject();
		JSONObject queston;
		JSONArray sub_data = new JSONArray();
		for (QuestionRequest q : submit_data) {
			queston = new JSONObject();
			queston.put(NetworkUtility.QUESTIONID, q.getQuestionID());
			queston.put(NetworkUtility.ANSWERTYPE, q.getAnswerType());
			queston.put(NetworkUtility.ANSWERID, q.getAnswerID());
			queston.put(NetworkUtility.ANSWERTEXT, q.getAnswerText());
			queston.put(NetworkUtility.STATUS, q.getStatus());
			sub_data.put(queston);
		}
		data.put(NetworkUtility.HANDLE, NetworkUtility.HANDLE_SUBMIT_CONTEST);
		data.put(NetworkUtility.USER_ID, user_id);
		data.put(NetworkUtility.CONTEST_ID, contest_id);
		data.put(NetworkUtility.DATA, sub_data);
		return data.toString();
	}

}
